#include "account/account.h"
#include "pjsipwrapper/pjsipwrapper.h"
#include "session/userhandler.h"

#include "userprofile.h"
#include "utils/logger.h"

UserProfile* UserProfile::m_userProfile = 0;
QMutex UserProfile::m_mutex;

UserProfile::UserProfile()
:m_account(0), m_userHandler(0), m_accountSet(false)
{
    LOG_DEBUG("start to init the PjApiUa");
    PjApiUa::getInstance()->init();
    PjApiUa::getInstance()->start();


}

UserProfile::~UserProfile()
{
    m_accountSet = false;
    if(m_account) delete m_account;
    if(m_userHandler) delete m_userHandler;
    PjApiUa::getInstance()->terminate();
}



int UserProfile::setupAccount(std::string username, std::string password)
{
    if(username == "" || password == "")
    {
        return -1;
    }
    m_username = username;
    m_password = password;

    initAccount(username, password);
    initUserHandler();

    m_accountSet = true;

    //PjApiUa::getInstance()->start();
    return 0;
}

int UserProfile::initAccount(std::string username, std::string password)
{
    m_account = new Account(username, password);
    return 0;
}

int UserProfile::initUserHandler()
{
    if(m_account != NULL) m_userHandler = new UserHandler(m_account);
    PjApiUa::getInstance()->registerAccountHandler(m_userHandler->getPjApiHandler());

    if(!m_userHandler || !m_userHandler->getPjApiHandler()) return -1;
    m_userHandler->init();

    m_userHandler->getPjApiHandler()->setNullDevice();
   // return 0;
}
